<?php
declare (strict_types = 1);

namespace app\home\controller;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use app;
use app\BaseController;

class Goods extends BaseController
{
    //
    public function index(){
        //注意：控制器里的类对应view下面的目录，控制器里的方法对应view下面静态的文件
        // dump(Request::param('name','','trim,md5')); 
        // 渲染视图
        // return View::fetch();
        // 模板变量赋值
        // View::assign([
        //     'name'=>'ThinkPHP',
        //     'email'=>'thinkphp.com',
        //     'pwd'=>'1213'
        // ]);
        // return View::fetch('index',[
        //     'name'=>'ThinkPHP',
        //     'email'=>'thinkphp.com',
        //     'pwd'=>'1213'
        // ]);
        // 渲染视图

        // return View::fetch();
        

        //操作数据库
        //select方法查询结果是一个数据集对象，可以转为数组toArray()
        // $data=Db::table('goods')->where('id','1')->select()->toArray();
        
        return View::fetch();
    }
    public function add(){
        // return View::fetch('add',['name'=>'lili','email'=>'zbc','pwd'=>'xjj']);
        // 助手函数
                // return view('add',['name'=>'lili','email'=>'zbc','pwd'=>'xjj']);
        
        // $data=['proID'=>'78','proname'=>'汉堡包','proguige'=>'一个','proprice'=>'8.0000'];
        // Db::table('products')->insert($data);
        return View::fetch('add');
    }
    public function add1(){
        $data=Request::param();
        // dump($data);
        // return View::fetch('add1');
        $result=['proID'=>null,'proname'=>$data['name'],'proguige'=>$data['guige'],'proprice'=>$data['price']];
        $insert=Db::table('products')->insert($result);
        if($insert){
            echo "<script>alert('chenggong啊')</script>";
        }else{
            echo "<script>alert('补药啊')</script>";
        }
        // Db::name('products')->save($result);
        $data=Db::table('products')->select()->toArray();
            
        // dump($data);
        View::assign('data',$data);
        
        return View::fetch('index');
    }
    
    public function edit(){
        // return View::fetch('edit');
        $id=Request::param('proID');
        
        $data=Db::table('products')->where('proID',$id)->find();
        // dump($data);
        return View::fetch('edit',['data'=>$data]);
        // $result=[''=>''];
    }
    public function edit1(){
        $data=Request::param();
        $id=Request::param('proID');
        // dump($data);
        $result=['proname'=>$data['name'],'proguige'=>$data['guige'],'proprice'=>$data['price']];
        Db::table('products')->where('proID',$id)->update($result);
        // dump($result);
        $data=Db::table('products')->select()->toArray();
            
        // dump($data);
        View::assign('data',$data);
        
        return View::fetch('index');
    }
    public function del(){
        $data=Request::param();
        // dump($id);
        Db::table('products')->where('proID',$data['proID'])->delete();
        $data=Db::table('products')->select()->toArray();
            
        // dump($data);
        View::assign('data',$data);
        return View::fetch('index');
    }
}
